'''
此文件为解析各文件中的参数内容，用于参数化设置。
包含INI文件、TXT文件、CSV文件、YAML文件；
'''
import os
from Config.Config_file_Path import data_path
from TongYong.Log_Info import Log
import Decode_ini_yaml
from TongYong.Db_created import Db_created

class Get_data_files:
    '''
    首先定义一个构造函数，将文件名进行
    '''
    def __init__(self, filename):
        print("第一步？")
        newfilename = os.path.join(data_path, filename)
        if not os.path.isfile(newfilename):
            Log.error("参数化文件不存在！{}", newfilename)
            raise FileNotFoundError
        self.newfilename = newfilename

    @property
    def get_ini_file(self):
        global data_list
        get_data = []
        Ini_data = Decode_ini_yaml.Decode_ini.Decode_ini_file(self.newfilename)
        test_data = dict(Ini_data)
        print("test_data:", test_data)
        for data in test_data:
            data_list = list(test_data[data].keys())
            print("data_list:", data_list)
            get_data.append(list(test_data[data].values()))
        print("测试：", ','.join(data_list), get_data)
        return ','.join(data_list), get_data

    @property
    def get_yaml_file(self):
        get_data = []
        yaml_data = Decode_ini_yaml.Decode_yaml.Decode_yaml_file(self.newfilename)
        data_list = list(yaml_data[0].keys())
        print("data_list:", data_list)
        for data_one in yaml_data:
            get_data.append(list(data_one.values()))
        print("s数据：", ','.join(data_list), get_data)
        return ','.join(data_list), get_data

    @property
    def get_txt_csv_file(self):
        global data_list
        index = 0
        print("开始")
        with open(self.newfilename, encoding="utf-8") as fr_txt_csv:
            get_data = []
            for oneline in fr_txt_csv:
                oneline = oneline.strip()
                print(oneline)
                if oneline:
                    if index == 0:
                        data_list = oneline.split(',')
                        print("data_list_01:",data_list)
                        index += 1
                    else:
                        get_data.append(oneline.split(','))
                        print(get_data)
                print("测试：", ','.join(data_list), get_data)
            return ','.join(data_list), get_data

    @staticmethod
    def form_mysql(sql):
        get_data = []
        mysql = Db_created().mysql_created(data_type=2) #[{}.{}]
        data = mysql.fetchall(sql)
        fields = list(data[0].keys())
        for d in data:
            get_data.append(list(d.values()))
        return ','.join(fields), get_data

# ceshi = Get_data_files("test.ini")
# x = ceshi.get_ini_file
# print("x:",x)
# ceshi_01 = Get_data_files("login.txt")
# y = ceshi_01.get_txt_csv_file
# print("y:",y )
